package com.lyk.jsbridge.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lyk.jsbridge.Constant;
import com.lyk.jsbridge.R;
import com.lyk.jsbridge.http.MyOkhttp;
import com.lyk.jsbridge.http.callback.BitmapCallback;
import com.lyk.jsbridge.http.callback.StringNoDialogCallback;
import com.lyk.jsbridge.http.request.BaseRequest;
import com.lyk.jsbridge.httpmodel.LoginBean;
import com.lyk.jsbridge.httpmodel.RegisterBean;
import com.lyk.jsbridge.httpmodel.RegisterCode;
import com.lyk.jsbridge.util.Regular;
import com.lyk.jsbridge.util.TimeCount;
import com.lyk.jsbridge.util.ToastUtil;
import com.lyk.jsbridge.util.VolleySingleton;
import com.lyk.jsbridge.vo.VolleyError;
import com.lyk.jsbridge.vo.toolbox.ImageRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/3/8.
 */

public class RegisterActivity extends BaseActivty2 {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_top_layout)
    RelativeLayout rlTopLayout;
    @BindView(R.id.user_phone_num)
    EditText userPhoneNum;
    @BindView(R.id.img_clear_user)
    ImageView imgClearUser;
    @BindView(R.id.user_imgcaptcha)
    EditText userImgcaptcha;
    @BindView(R.id.img_imgcaptcha)
    ImageView imgImgcaptcha;
    @BindView(R.id.rl_imgcaptcha)
    RelativeLayout rlImgcaptcha;
    @BindView(R.id.user_captcha)
    EditText userCaptcha;
    @BindView(R.id.btn_send_mes)
    Button btnSendMes;
    @BindView(R.id.tv_voice_code)
    TextView tvVoiceCode;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.btn_next)
    Button btnNext;
    private TimeCount timeCount;

    //       img_imgcaptcha.setImageBitmap(response);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
//        Glide.with(RegisterActivity.this).load(Constant.URL + "captcha.png").error(R.mipmap.logo).placeholder(R.mipmap.logo).into((imgImgcaptcha));
        initcaptcha();
        timeCount = new TimeCount(60000, 100, btnSendMes);


        userPhoneNum.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (userPhoneNum.getText().toString().equals("")) {
                    imgClearUser.setVisibility(View.GONE);
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    imgClearUser.setVisibility(View.VISIBLE);
                }

                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (userCaptcha.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });

        userImgcaptcha.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (userImgcaptcha.getText().toString().equals("")) {
                    imgImgcaptcha.setVisibility(View.GONE);
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    imgImgcaptcha.setVisibility(View.VISIBLE);
                }

                if (TextUtils.isEmpty(userImgcaptcha.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (userCaptcha.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });

        userCaptcha.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (userPhoneNum.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else if (userCaptcha.getText().toString().equals("")) {

                    btnNext.setEnabled(false);
                    btnNext.setBackgroundResource(R.drawable.btn_unclick);
                } else {
                    btnNext.setEnabled(true);
                    btnNext.setBackgroundResource(R.drawable.v6button);
                }

            }
        });


    }

    @OnClick({R.id.img_back, R.id.rl_back, R.id.tv_title, R.id.rl_top_layout, R.id.user_phone_num, R.id.img_clear_user, R.id.user_imgcaptcha, R.id.img_imgcaptcha, R.id.rl_imgcaptcha, R.id.user_captcha, R.id.btn_send_mes, R.id.tv_voice_code, R.id.tv_comment, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_title:

                break;
            case R.id.rl_top_layout:

                break;
            case R.id.user_phone_num:
                break;
            case R.id.img_clear_user:
                userPhoneNum.setText("");
                break;
            case R.id.user_imgcaptcha:
                break;
            case R.id.img_imgcaptcha:
//                Glide.with(RegisterActivity.this).load(Constant.URL + "captcha.png").error(R.mipmap.logo).placeholder(R.mipmap.logo).into((imgImgcaptcha));
                userImgcaptcha.setText("");
                break;
            case R.id.rl_imgcaptcha:
                initcaptcha();
                break;
            case R.id.user_captcha:
                break;
            case R.id.btn_send_mes:
                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    ToastUtil.showCenterToast(RegisterActivity.this, "手机号不能为空");
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    ToastUtil.showCenterToast(RegisterActivity.this, "请输入正确的手机号码");
                } else {

                    getCode();

                }


                break;
            case R.id.tv_voice_code:
                break;
            case R.id.tv_comment:
                break;
            case R.id.btn_next:

                if (TextUtils.isEmpty(userPhoneNum.getText().toString())) {
                    ToastUtil.showCenterToast(RegisterActivity.this, "手机号不能为空");
                } else if (!Regular.isMobileNO(userPhoneNum.getText().toString())) {
                    ToastUtil.showCenterToast(RegisterActivity.this, "请输入正确的手机号码");
                } else {
                    valid();
                }


//                register();


                break;
        }
    }

    public void getCode() {
        String url = Constant.URL + "api/v1/system/getMobileCaptcha/18888888888/0";

        MyOkhttp.get(url)
                .tag(this)

                .execute(new StringNoDialogCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        timeCount.start();
                        showLoading();

                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        Gson gson = new Gson();

//


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);

                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);
                        dismissLoading();
//                        timeCount.cancel();
                    }
                });
    }

    private void initcaptcha() {
        // TODO Auto-generated method stub


        ImageRequest imageRequest = new ImageRequest(Constant.URL + "/captcha.png", new com.lyk.jsbridge.vo.Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                Log.d(response.toString(), "3333333333333333333");
                imgImgcaptcha.setImageBitmap(response);
            }
        }, 0, 0, Bitmap.Config.RGB_565, new com.lyk.jsbridge.vo.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                imgImgcaptcha.setImageResource(R.mipmap.ic_launcher);
            }
        });
        VolleySingleton.getInstance(RegisterActivity.this).addToRequestQueue(imageRequest);

    }

    public void valid() {
//        userImgcaptcha
        String myurl = Constant.URL + "api/v1/system/validatePictureCaptcha/" + "1234";
//        userImgcaptcha.getText().toString()
        MyOkhttp.get(myurl)
                .tag(this)


                .execute(new StringNoDialogCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);


                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
//                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                        Gson gson = new Gson();
//                        Toast.makeText(RegisterActivity.this, "成功", Toast.LENGTH_SHORT).show();
//                        startActivity(new Intent(RegisterActivity.this, RegisterSettingsActivity.class));
                        valid_smsCode();

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Log.d(response.toString(), "333333333333");

//                        Toast.makeText(RegisterActivity.this, "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);

                    }
                });
    }


    public void valid_smsCode() {
//        userImgcaptcha
        String myurl = Constant.URL + "api/v1/system/validatePictureCaptcha/" + "1234";
//        userImgcaptcha.getText().toString()
        MyOkhttp.get(myurl)
                .tag(this)
                .execute(new StringNoDialogCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);


                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
//                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                        Gson gson = new Gson();
//                        Toast.makeText(RegisterActivity.this, "成功", Toast.LENGTH_SHORT).show();
//                        startActivity(new Intent(RegisterActivity.this, RegisterSettingsActivity.class));

                        Intent intent = new Intent(RegisterActivity.this, RegisterSettingsActivity.class);
                        intent.putExtra("phone", userPhoneNum.getText().toString());
                        startActivity(intent);
                        finish();

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Log.d(response.toString(), "333333333333");

//                        Toast.makeText(RegisterActivity.this, "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);

                    }
                });
    }

    public void register() {
        MyOkhttp.post(Constant.URL + "api/v1/system/registerSubmit")
                .tag(this)
                .params("mobile", userPhoneNum.getText().toString())
                .params("password", "123456")

                .execute(new StringNoDialogCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);


                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
//                        Log.d(s, "eeeeeeeeeeeeeeeeeeeee");
                        Gson gson = new Gson();

                        RegisterBean registerBean = new RegisterBean();
                        registerBean = gson.fromJson(s, RegisterBean.class);
                        if (registerBean.getMeta().isSuccess() == false) {
                            Toast.makeText(RegisterActivity.this, registerBean.getMeta().getMessage() + "", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(RegisterActivity.this, registerBean.getMeta().getMessage() + "", Toast.LENGTH_SHORT).show();
                        }


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Log.d(response.toString(), "333333333333");

                        Toast.makeText(RegisterActivity.this, "error", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAfter(String s, Exception e) {
                        super.onAfter(s, e);

                    }
                });
    }
}
